/*
 * DxfEntityCounter.java
 *
 * Created on 6 de Dezembro de 2007, 9:48
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package importadorDXF;

import utility.Counter;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author rafaelbuck
 */
public final class DxfEntityCounter
{

    private static String getDxfEntityName(Class cl)
    {
        if(cl == entityClass)
            return null;
        String className = cl.getName();
        int index = className.lastIndexOf(".Dxf");
        if(index == -1)
            return null;
        else
            return className.substring(index + 4);
    }

    public void add(DxfEntity ent)
    {
        Class cl = ent.getClass();
        String name = (String)class2name.get(cl);
        if(name == null)
        {
            name = getDxfEntityName(cl);
            if(name == null)
                return;
            class2name.put(cl, name);
        }
        add(name);
    }

    public void add(String name)
    {
        Counter cnt = (Counter)counts.get(name);
        if(cnt == null)
        {
            cnt = new Counter(1);
            counts.put(name, cnt);
        } else
        {
            cnt.add1();
        }
    }

    public int getCount(DxfEntity ent)
    {
        Class cl = ent.getClass();
        String name = (String)class2name.get(cl);
        if(name == null)
            name = getDxfEntityName(cl);
        return getCount(name);
    }

    public int getCount(String name)
    {
        if(name == null)
        {
            return 0;
        } else
        {
            Counter cnt = (Counter)counts.get(name);
            return cnt != null ? cnt.getValue() : 0;
        }
    }

    public Enumeration getEntityNames()
    {
        return counts.keys();
    }

    /** Creates a new instance of DxfEntityCounter */
    public DxfEntityCounter()
    {
        class2name = new Hashtable(23);
        counts = new Hashtable(23);
    }

    private static Class entityClass = (new DxfEntity()).getClass();
    private Hashtable class2name;
    private Hashtable counts;

}